package session.TX;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.*;
import Helper.Helper_Outlet;
import Helper.Helper_Stakeholder_Food;
import java.util.ArrayList;
import javax.persistence.Query;

/**
 *
 * @author TWD
 */
@Stateless
public class StakeholderAccountSB implements StakeholderAccountsSBRemote {

    @PersistenceContext()
    EntityManager em;
    private Stakeholder stakeholder;
    private StakeholderFood stakeholderFood;
    private StakeholderTier tier;

    public StakeholderAccountSB() {
    }

    public boolean createStakeholder(String name, Integer loyaltyPoints, String email, String stakeholderTier) {

        if (checkStakeholderName(name)) {
            stakeholderTier = stakeholderTier.toUpperCase();
            tier = em.find(StakeholderTier.class, stakeholderTier);
            stakeholder = new Stakeholder();
            stakeholder.create(name, loyaltyPoints, tier, email);
            em.persist(stakeholder);
            return true;
        } else {
            return false;
        }

    }

    public boolean createStakeholderFood(String name, Integer loyaltyPoints, String email, String stakeholderTier) {

        if (checkStakeholderName(name)) {
            //Checking the validity of the name and the tier
            stakeholderTier = stakeholderTier.toUpperCase();
            tier = em.find(StakeholderTier.class, stakeholderTier);

            //Setting the Stakeholder entity
            stakeholderFood = new StakeholderFood();
            stakeholderFood.setName(name);
            stakeholderFood.setEmail(email);
            stakeholderFood.setLoyaltyPoints(loyaltyPoints);
            stakeholderFood.setStakeholderTier(tier);
            em.persist(stakeholderFood);
            return true;
        } else {
            return false;
        }

    }

    public boolean checkStakeholderName(String name) {

        stakeholder = em.find(Stakeholder.class, name);
        if (stakeholder == null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean deleteStakeholder(String name) {
        boolean bool = true;
        stakeholder = em.find(Stakeholder.class, name);
        if (stakeholder == null) {
            return false;
        } else {
            Query q1 = em.createQuery("SELECT c FROM StakeholderBill c WHERE c.stakeHolderName = :paramName");
            q1.setParameter("paramName", name);

            List resultList = q1.getResultList();

            if (!resultList.isEmpty()) {
                for (Object o : q1.getResultList()) {
                    StakeholderBill bill = (StakeholderBill) o;
                    if (bill.isHasPaid()) {
                        bool = false;
                    }
                }
            }
            if (bool == true) {
                stakeholder.setStakeholderTier(null);
                em.remove(stakeholder);
                System.out.println("StakeholderAccountsBean: Stakeholder deleted: " + stakeholder.getName());
                return true;
            } else {
                return false;
            }
        }
    }

    public boolean changeStakeholderTier(String stringTier, String name) {
        try {

            tier = em.find(StakeholderTier.class, stringTier);
            stakeholder = em.find(Stakeholder.class, name);
            stakeholder.setStakeholderTier(tier);
            em.flush();
            return true;

        } catch (Exception ex) {
            return false;
        }
    }

    public List<Stakeholder> getAllStakeholders() {
        Query query = em.createQuery("SELECT e FROM Stakeholder e");
        return query.getResultList();
    }

    public List<Helper_Stakeholder_Food> getAllStakeholderFoodHelper() {
        Query query = em.createQuery("SELECT s FROM StakeholderFood s");
        List<StakeholderFood> stakeholders = query.getResultList();
        List<Helper_Stakeholder_Food> returnList = new ArrayList<Helper_Stakeholder_Food>();
        for (StakeholderFood s : stakeholders) {
            Helper_Stakeholder_Food aStakeholder = new Helper_Stakeholder_Food(s.getName());
            returnList.add(aStakeholder);
            List<Outlet> outs = (List<Outlet>) s.getOutlet();
            if (outs == null) {
                continue;
            }
            for (Outlet o : outs) {
                aStakeholder.addOutlet(new Helper_Outlet(o.getId(), o.getOutletName(),
                        o.getAddress(), s.getName(), o.getPhoneNumber()));
            }
        }
        return returnList;
    }

    public List<Stakeholder> getSpecificStakeholders(String name) {
        Query query = em.createQuery("SELECT OBJECT(e) FROM Stakeholder e where LOCATE(:name, e.name)>0");
        query.setParameter("name", name);
        return query.getResultList();
    }

    public List<Helper_Outlet> getOutletsByStakeholder(String sname) {
        System.out.println("sname: " + sname);
        StakeholderFood s = em.find(StakeholderFood.class, sname);
        List<Outlet> list = s.getOutlet();
        List<Helper_Outlet> returnList = new ArrayList<Helper_Outlet>();
        for (Outlet o : list) {
            returnList.add(new Helper_Outlet(o.getId(), o.getOutletName(), o.getAddress(),
                    s.getName(), o.getPhoneNumber()));
        }
        System.out.println(returnList);
        return returnList;
    }

    //Randall added: 19-oct-2011
    public void givePointsToStakeholder(String stakeholderName, int pointsToGive) {
        System.out.println(">>> StakeholderAccountsBean: Awarding points to stakeholder...");
        stakeholder = em.find(Stakeholder.class, stakeholderName);
        if (stakeholder == null) {
            System.out.println(">>> StakeholderAccountsBean: Unable to find stakeholder");
            return;
        }
        int points = stakeholder.getLoyaltyPoints() + pointsToGive;
        stakeholder.setLoyaltyPoints(points);
        em.flush();
    }
}
